package com.hxkcloud.nurse.fragments;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.YjhApplication;
import com.hxkcloud.nurse.activitys.ChartDetailActivity;
import com.hxkcloud.nurse.activitys.MessageMainActivity;
import com.hxkcloud.nurse.activitys.MonitorInputActivity;
import com.hxkcloud.nurse.adapter.Chart1Adapter;
import com.hxkcloud.nurse.beans.LineListBean;
import com.hxkcloud.nurse.beans.MyChartListBean;
import com.hxkcloud.nurse.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.StringUtil;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by ray_lei on 2016/8/8.
 */
public class Monitor1Fragment extends BaseFragment implements View.OnClickListener, Chart1Adapter.ResultSelectedListener, PullToRefreshLayout.OnRefreshListener {

    private View view;

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private ImageView iv_message;
    private ImageView iv_msgpoint;

    private LinearLayout ll_empty;
    private LinearLayout ll_info;

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private List<LineListBean> chartInfoList;
    private Chart1Adapter adapter;

    private String patientId = "";

    private ObjectResultBean<MyChartListBean> resultBean;


    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_GETMONITORDAYDATA_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<MyChartListBean>) msg.obj;
                    if (resultBean != null) {
                        chartInfoList.clear();
                        chartInfoList = resultBean.getData().getChartList();
                        adapter = new Chart1Adapter(getActivity(), chartInfoList, recycleView, this, loginInfo.getPatientId(), loginInfo.getUserStyle().equals(YjhApplication.userstyle_patient), screenWidth);
                        recycleView.setAdapter(adapter);
//                        adapter.setData(chartInfoList);
//                        toastMsg(resultBean.getMessage());
                    }


                }

                break;
            case MessageCode.NETWORKCODE_GETMONITORDAYDATA_FAILURE:
                dismisssLoading();
                ll_empty.setVisibility(View.VISIBLE);
                ll_info.setVisibility(View.GONE);
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

//                chartInfoList.clear();
//                adapter = new Chart1Adapter(getActivity(), chartInfoList, recycleView, this, loginInfo.getPatientId(), loginInfo.getUserStyle().equals(YjhApplication.userstyle_patient), screenWidth);
//                recycleView.setAdapter(adapter);

                break;

        }

    }

    public void showPoint(boolean show) {
        if (iv_msgpoint != null)
            if (show) {
                iv_msgpoint.setVisibility(View.VISIBLE);
            } else {
                iv_msgpoint.setVisibility(View.INVISIBLE);
            }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_monitor, container, false);

        chartInfoList = new ArrayList<LineListBean>();

        ll_title = (LinearLayout) view.findViewById(R.id.ll_title);
        iv_back = (ImageView) view.findViewById(R.id.iv_title_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title_title);
        iv_message = (ImageView) view.findViewById(R.id.iv_title_func);
        iv_msgpoint = (ImageView) view.findViewById(R.id.iv_title_point);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.INVISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText("  " + getResources().getText(R.string.monitor_text));
        iv_message.setVisibility(View.VISIBLE);
        iv_message.setImageResource(R.mipmap.nav_message);
        iv_message.setOnClickListener(this);

        ll_empty = (LinearLayout) view.findViewById(R.id.ll_department_empty);
        ll_info = (LinearLayout) view.findViewById(R.id.ll_monitor_info);

        refreshLayout = (PullToRefreshLayout) view.findViewById(R.id.monitor_chart_refreshlayout);
        recycleView = (PullableRecyclerView) view.findViewById(R.id.monitor_chart_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);


        initView();
        initListener();

        return view;
    }

    public void isMonitorReady() {
        if (chartInfoList != null && recycleView != null) {
            adapter = new Chart1Adapter(getActivity(), chartInfoList, recycleView, this, loginInfo.getPatientId(), loginInfo.getUserStyle().equals(YjhApplication.userstyle_patient), screenWidth);
            recycleView.setAdapter(adapter);

            initData();
        }
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void initData() {
        patientId = loginInfo.getPatientId();
        loading();
        YiJiaHuNetWorkUtils.getMonitorDayData(patientId, handler);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_func:
                if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
                    Intent iM = new Intent(mContext, MessageMainActivity.class);
                    startActivity(iM);
                } else {
                    toastMsg(R.string.pleasefirstjoindepartment);
                }
                break;
        }
    }

    @Override
    public void onSelected(int position, String type) {
        if ("input".equals(type)) {
            StringUtil.zhugeTrack(mContext, chartInfoList.get(position).getName() + "录入保存按钮", chartInfoList.get(position).getName() + "录入保存按钮点击数");

            Intent intent = new Intent(mContext, MonitorInputActivity.class);
            intent.putExtra("monitorId", chartInfoList.get(position).getKindId());
            startActivityForResult(intent, 111);
        }
        if ("itemClick".equals(type)) {
            Intent intent = new Intent(mContext, ChartDetailActivity.class);
            intent.putExtra("patientId", patientId);
            intent.putExtra("kindId", chartInfoList.get(position).getKindId());
            intent.putExtra("name", chartInfoList.get(position).getName());
            intent.putExtra("time", chartInfoList.get(position).getDate());
            intent.putExtra("min", chartInfoList.get(position).getMinValue());
            intent.putExtra("max", chartInfoList.get(position).getMaxValue());
            if (chartInfoList.get(position).getLineList() != null) {
                for (int i = 0; i < chartInfoList.get(position).getLineList().size(); i++) {
                    intent.putExtra("text" + i, chartInfoList.get(position).getLineList().get(i).getName());
                }
            }
            startActivityForResult(intent, 111);
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 111) {
                initData();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(loginInfo.getDepartmentId())) {
            ll_empty.setVisibility(View.VISIBLE);
            ll_info.setVisibility(View.GONE);
        } else {
//            if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
//                initData();
//            }
            ll_empty.setVisibility(View.GONE);
            ll_info.setVisibility(View.VISIBLE);
        }

        if (YjhApplication.msg_new) {
            iv_msgpoint.setVisibility(View.VISIBLE);
        } else {
            iv_msgpoint.setVisibility(View.INVISIBLE);
        }
    }


    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        initData();
        refreshLayout
                .refreshFinish(PullToRefreshLayout.SUCCEED);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        refreshLayout
                .loadmoreFinish(PullToRefreshLayout.SUCCEED);
    }


}
